//
//  CDSelectLogoDetialViewController.m
//  CollectToDesktop
//
//  Created by 王启颖 on 2020/10/17.
//  Copyright © 2020 godbe. All rights reserved.
//

#import "CDSelectLogoDetialViewController.h"
#import "CDMoreLogoCell.h"
@interface CDSelectLogoDetialViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) NSMutableArray *nameArr;
@end

@implementation CDSelectLogoDetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self collectionView];
}

- (void)setNameID:(NSNumber *)nameID{
    _nameID = nameID;
    [self request];
}

#pragma mark - 网络请求
-(void)request{

    NSString *urlstr=[NSString stringWithFormat:@"https://gapi.szlzyd.com/0010/na/icon/icon"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    dic[@"iconClassificationId"] = self.nameID;
    [[CDNetworkRequset HttpRequstManager] PostURL:urlstr params:dic success:^(NSInteger code, id  _Nonnull result) {

        
        self.nameArr = result[@"data"];
        [self.collectionView reloadData];
        
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.self.nameArr == 0) {
        return self.dataArr.count;
    }else{
        return self.nameArr.count;
    }
    
}


- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    CDMoreLogoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kCDMoreLogoCellID" forIndexPath:indexPath];
    
    
    if (self.self.nameArr == 0) {
        NSDictionary *dic = self.dataArr[indexPath.row];
        cell.logoImg.image = [UIImage imageNamed:dic[@"logoPic"]];
    }else{
        NSDictionary *dic = self.nameArr[indexPath.row];
        [cell.logoImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dic[@"iconUrl"]]] placeholderImage:[UIImage imageNamed:@"1234"]];
    }
    return cell;
}

#pragma mark - UICollectionViewDelegateLeftAlignedLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake((kDeviceWidth - 60 -36)/4, (kDeviceWidth - 60 -36)/4);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    [self setupUI:dic];
    
    if (self.self.nameArr == 0) {
        NSDictionary *dic = self.dataArr[indexPath.row];
        !self.logoClickBlock?:self.logoClickBlock([UIImage imageNamed:dic[@"logoPic"]]);
    }else{
        NSDictionary *dic = self.nameArr[indexPath.row];
        !self.logoClickBlock?:self.logoClickBlock([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"iconUrl"]]]]);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 懒加载
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.sectionInset = UIEdgeInsetsMake(18, 18, 18, 18);
        layout.minimumLineSpacing = 20;
        layout.minimumInteritemSpacing = 20;

        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollsToTop = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerNib:[UINib nibWithNibName:@"CDMoreLogoCell" bundle:nil] forCellWithReuseIdentifier:@"kCDMoreLogoCellID"];

        [self.view addSubview:_collectionView];

    }
    return _collectionView;
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
-(NSMutableArray *)nameArr{
    if (!_nameArr) {
        _nameArr = [[NSMutableArray alloc]init];
    }
    return _nameArr;
}

@end
